package com.yuzhenxin.coursesystem.listener;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.yuzhenxin.coursesystem.entity.StudentDO;
import com.yuzhenxin.coursesystem.service.StudentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
public class StudentListener extends AnalysisEventListener<StudentDO> {

    private final StudentService studentService;

    private final List<StudentDO> studentEntityList = new ArrayList<>();

    @Override
    public void invoke(StudentDO StudentEntity, AnalysisContext analysisContext) {
        studentEntityList.add(StudentEntity);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (ObjectUtil.isNotEmpty(studentEntityList)) {
            studentService.saveBatch(studentEntityList);
            log.info("保存数据成功");
        }
    }
}